from .utils import *
from praatio import pitch_and_intensity
from praatio import praat_scripts

def pitch(wavFN, outputFN, 
         minPitch=50, maxPitch=450, sampleStep=0.01,
         silenceThreshold=0.03, forceRegenerate=True,
         undefinedValue=0.00, medianFilterWindowSize=0,
         pitchQuadInterp=False):
    '''
    Extract pitch at regular intervals from the input wav file
    
    Data is output to a text file and then returned in a list in the form
    [(timeV1, pitchV1), (timeV2, pitchV2), ...]
    
    sampleStep - the frequency to sample pitch at
    silenceThreshold - segments with lower intensity won't be analyzed
                       for pitch
    forceRegenerate - if running this function for the same file, if False
                      just read in the existing pitch file
    undefinedValue - if None remove from the dataset, otherset set to
                     undefinedValue
    pitchQuadInterp - if True, quadratically interpolate pitch
    '''

    wavFN = os.path.abspath(wavFN)
    outputFN = os.path.abspath(outputFN)
    praatEXE = get_bin('praat')

    return pitch_and_intensity.extractPitch(wavFN, outputFN, praatEXE,
                 minPitch, maxPitch, 
                 sampleStep=sampleStep,
                 silenceThreshold=silenceThreshold, 
                 forceRegenerate=forceRegenerate,
                 undefinedValue=undefinedValue, 
                 medianFilterWindowSize=medianFilterWindowSize,
                 pitchQuadInterp=pitchQuadInterp)

def intensity(inputFN, outputFN,
             minPitch=50, sampleStep=0.01, forceRegenerate=True,
             undefinedValue=0.00):
    
    inputFN = os.path.abspath(inputFN)
    outputFN = os.path.abspath(outputFN)
    praatEXE = get_bin('praat')

    return pitch_and_intensity.extractIntensity(inputFN, outputFN, praatEXE,
                     minPitch, 
                     sampleStep=sampleStep, 
                     forceRegenerate=forceRegenerate,
                     undefinedValue=undefinedValue)

def formants(inputWavFN, outputTxtFN, maxFormant=5500,
                stepSize=0.01, window_length=0.025, preemphasis=50,
                scriptFN=None, undefinedValue=0.00):
    '''
    Get F1, F2, and F3 for the audio file
    
    maxFormant = 5500 for females, 5000 for males, <8000 for children
    '''

    inputWavFN = os.path.abspath(inputWavFN)
    outputTxtFN = os.path.abspath(outputTxtFN)
    praatEXE = get_bin('praat')

    return praat_scripts.getFormants(praatEXE, inputWavFN, outputTxtFN, maxFormant,
                stepSize=stepSize, window_length=window_length, preemphasis=preemphasis,
                scriptFN=scriptFN, undefinedValue=undefinedValue)